package org.jeecg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.http.util.EntityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.modules.dic.service.IDicBeGoodAtService;
import org.jeecg.common.modules.doctor.entity.*;
import org.jeecg.common.modules.doctor.service.*;
import org.jeecg.common.modules.doctor.vo.DoctorRegisterRecordSysListVo;
import org.jeecg.common.modules.doctor.vo.DoctorRegisterVo;
import org.jeecg.common.modules.doctor.vo.DoctorReqVo;
import org.jeecg.common.modules.doctorInfoTemp.service.IDoctorInfoTempService;
import org.jeecg.common.modules.register.entity.RegisterPayOrder;
import org.jeecg.common.modules.register.service.IRegisterPayOrderService;
import org.jeecg.common.modules.registerRefoundAudit.entity.RegisterRefoundAudit;
import org.jeecg.common.modules.registerRefoundAudit.service.IRegisterRefoundAuditService;
import org.jeecg.common.modules.wx.entity.WxAccount;
import org.jeecg.common.modules.wx.entity.WxUserinfo;
import org.jeecg.common.modules.wx.service.IWxAccountService;
import org.jeecg.common.modules.wx.service.IWxUserinfoService;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.common.util.GetUtils;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.enums.RegisterEnum;
import org.jeecg.modules.data.service.PageReturn;
import org.jeecg.modules.system.vo.ResultVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RequestParam;
import org.weixin4j.spring.WeixinTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Wrapper;
import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class InitDoctorController {
    @Resource  //待添加的医生表
    private IDoctorInfoTempService iDoctorInfoTempService;
    @Resource
    public IDoctorService doctorService;
    @Resource
    private IDoctorExtendService doctorExtendService;
    @Resource
    private IDoctorConfigService doctorConfigService;
    @Resource
    private IRegisterPayOrderService registerPayOrderService;
    @Resource
    private IWxUserinfoService wxUserinfoService;
    @Autowired
    private IRegisterRefoundAuditService registerRefoundAuditService;
    @Autowired
    private IDoctorRegisterService doctorRegisterService;
    @Autowired
    private IDoctorRegisterHospitalService doctorRegisterHospitalService;
    @Autowired
    private IDicBeGoodAtService dicBeGoodAtService;
    @Autowired
    private IDoctorRegisterRecordService doctorRegisterRecordService;
    @Resource
    private IWxAccountService wxAccountService;
    @Resource
    private WeixinTemplate weixinTemplate;
    @Test
    public void chufangDetails(){

        Map<String,Object> map = new HashMap<>();
        GetUtils getUtils = new GetUtils();
        map.put("access_token","55_FSAKddw5MfGymZl0volqGTXmNWhgfDr-2nA2fq3xVOTk9mPE5mJyZu6gj1zxS6ciRowRv9QL3ZBeKOcGjWS-qmzpi0BdFw_xrBVeWINIdgs");
        map.put("openid","oQj7Y6o4k3REnT0G6Bw_PDxiykW4");
        map.put("lang","zh_CN");
        String str = getUtils.getWithParams("https://api.weixin.qq.com/sns/userinfo",map);
        WxUserinfo wxUserinfo = new WxUserinfo();
        JSONObject jsonObject = JSON.parseObject(str);
        jsonObject.getString("nickname");
        wxUserinfo.setNickname(jsonObject.getString("nickname"));
        wxUserinfo.setSex(jsonObject.getInteger("sex"));
        wxUserinfo.setProvince(jsonObject.getString("province"));
        wxUserinfo.setCity(jsonObject.getString("city"));
        wxUserinfo.setCountry(jsonObject.getString("country"));
        wxUserinfo.setHeadimgurl(jsonObject.getString("headimgurl"));
        wxUserinfo.setUnionid(jsonObject.getString("unionid"));
        wxUserinfo.setOpenid(jsonObject.getString("openid"));
        WxUserinfo isHave = wxUserinfoService.getByOpenid(jsonObject.getString("openid"));
        if(isHave==null ||"".equals(isHave.getOpenid()) ){
            WxAccount wxAccount = wxAccountService.getByAppid(weixinTemplate.getWeixinConfig().getAppid());
            wxUserinfo.setWxAccountId(wxAccount.getAccountId());
            wxUserinfoService.save(wxUserinfo);
        }
        System.out.println("响应内容为:" + str);
    }



    //汉语转拼音,初始化账密
    @Test
    public void hanyuTopinyin() {
        Integer certificationStatus;
        String status = "2";
        if (status != null) {
            certificationStatus = Integer.valueOf(status);
        } else {
            certificationStatus = -1;
        }
        Page<DoctorReqVo> pageList = new Page<>(0, 10);
        pageList = doctorService.queryTcmDoctors(pageList, null, 2);
        System.out.println(pageList);
    }


    //处理擅长数据
    @Test
    public void chuli() {
        LambdaQueryWrapper<DoctorExtend> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.isNotNull(DoctorExtend::getGood);
        queryWrapper.isNull(DoctorExtend::getGoodDesc);
        List<DoctorExtend> list = doctorExtendService.list(queryWrapper);
        List<DoctorExtend> entends = new ArrayList<>();
        for (DoctorExtend doctorExtend : list) {
            String goodsDesc = "";
            String goods = doctorExtend.getGood();
            JSONArray jsonArray = JSONArray.parseArray(goods);
            if (jsonArray == null || jsonArray.equals("")) {
                continue;
            }
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                goodsDesc = goodsDesc + "," + object.get("name");
//                jsonBean myclass = JSONObject.parseObject(object.toJSONString() , jsonBean.class);// 将string类型直接封装成对象
            }
            System.out.println(goodsDesc);
            doctorExtend.setGoodDesc(goodsDesc.substring(1));
            entends.add(doctorExtend);
        }
        doctorExtendService.saveOrUpdateBatch(entends);
    }


    @Test
    public void addAll() {
        List res = new ArrayList();
        Doctor doctor = new Doctor();
        doctor.setName("杜翠云");
        doctor.setMobile("18012151234");
        doctor.setHospital("北京市丰台区铁营医院");
        doctor.setDepartment(21);
        doctor.setTitle("副主任医师");
        doctor.setSex(1);
        doctor.setProvinceId(2);
        doctor.setHeadImg(null);
        doctor.setCityId(52);
        doctor.setStatus(1);
        doctor.setCreateTime(new Date());
        doctor.setBusinessType(0);
        doctor.setCertificationStatus(2);
        doctor.setIsDisplay(1);
        doctor.setMaster(1);
        doctorService.save(doctor);

        //【二】医生配置信息保存.
        DoctorConfig doctorConfig = new DoctorConfig();
        doctorConfig.setDoctorId(doctor.getId());
        doctorConfig.setFreeMessageCount(5);
        doctorConfig.setTextMoney(null);
        doctorConfig.setVideoMoney(null);
        doctorConfigService.save(doctorConfig);

        //【三】医生扩展信息保存
        DoctorExtend doctorExtend = new DoctorExtend();
        doctorExtend.setDoctorId(doctor.getId());
        doctorExtend.setIntroduction("");
        doctorExtendService.save(doctorExtend);

        res.add(doctor.getId());
    }

    @Test
    public void updateRegisterStatus() {
        String id = "1447846493436239873";
        Integer invalid = 1;
        //伪删除挂号
        DoctorRegister doctorRegister = doctorRegisterService.getById(id);
        doctorRegister.setInvalid(invalid);
        doctorRegisterService.updateById(doctorRegister);
        //获取所有挂号记录
        List<DoctorRegisterRecord> doctorRegisterRecords = doctorRegisterRecordService.getDoctorRegisterRecord(id);
        //退款
        if (doctorRegisterRecords.size() > 0 && invalid != 1) {
            for (DoctorRegisterRecord recods : doctorRegisterRecords) {


                LambdaQueryWrapper<WxUserinfo> query = Wrappers.lambdaQuery();
                query.eq(WxUserinfo::getOpenid, recods.getOpenid());
                WxUserinfo wxUserinfo = wxUserinfoService.getOne(query); //获取微信用户
                RegisterPayOrder payOrder = new RegisterPayOrder();
                payOrder.setDoctorId(doctorRegister.getDoctorId());
                payOrder.setRelationId(recods.getId());
                payOrder.setStatus(RegisterEnum.OPERATE.getCode());
                payOrder.setWxUserinfoId(wxUserinfo.getId());
                RegisterPayOrder registerPayOrder = registerPayOrderService.getByRelationId(payOrder);

                //生成退款申请记录
                RegisterRefoundAudit registerRefoundAudit = new RegisterRefoundAudit();
                registerRefoundAudit.setOpenid(recods.getOpenid());
                registerRefoundAudit.setRegisterPayOrderId(registerPayOrder.getId());
                registerRefoundAudit.setStatus(0);
                registerRefoundAudit.setDoctorId(doctorRegister.getDoctorId());
                registerRefoundAudit.setRelationId(recods.getId());
                registerRefoundAuditService.save(registerRefoundAudit);
            }
        }
    }


    @Test
    public void queryPageList() {

        //分页数据开始的记录数
        int dataStart = 0;

        Integer status = null;
        //（1）先查询所有信息
        String doctorName = "";
        String patientName = "";
        String statusStr = "1";
        String visitTime = "";
        if (Objects.nonNull(statusStr)) {
            status = Integer.valueOf(statusStr);
        }

        List<DoctorRegisterRecordSysListVo> recordVos = doctorRegisterRecordService.getRegisterRecordSysList(doctorName, patientName, visitTime, status, dataStart, 10);

        //（2）获取总条数
        Integer count = doctorRegisterRecordService.getRegisterRecordSysCount(doctorName, patientName, visitTime, status);

        //（3）组织分页的返回数据
        PageReturn pageReturn = new PageReturn();
        ResultVo vo = pageReturn.initResultVo(recordVos, count, 1, 10);
        System.out.println(vo);
    }
}
